const fileCache = require('think-cache-file')
const nunjucks = require('think-view-nunjucks')
const fileSession = require('think-session-file')
const mysql = require('think-model-mysql')
const { Console, File, DateFile } = require('think-logger3')
const path = require('path')
const isDev = think.env === 'development'

/**
 * cache adapter config
 * @type {Object}
 */
exports.cache = {
  type: 'file',
  common: {
    timeout: 24 * 60 * 60 * 1000 * 30 // millisecond
  },
  file: {
    handle: fileCache,
    cachePath: path.join(think.ROOT_PATH, 'runtime/cache'), // absoulte path is necessarily required
    pathDepth: 1,
    gcInterval: 24 * 60 * 60 * 1000 // gc interval
  }
}

/**
 * session adapter config
 * @type {Object}
 */
exports.session = {
  type: 'file',
  common: {
    cookie: {
      name: 'thinkjs'
      // keys: ['werwer', 'werwer'],
      // signed: true
    }
  },
  file: {
    handle: fileSession,
    sessionPath: path.join(think.ROOT_PATH, 'runtime/session')
  }
}

/**
 * view adapter config
 * @type {Object}
 */
exports.view = {
  type: 'nunjucks',
  common: {
    viewPath: path.join(think.ROOT_PATH, 'view'),
    sep: '/',
    extname: '.html'
  },
  nunjucks: {
    handle: nunjucks
  }
}

exports.logger = {
  'type': isDev ? 'console' : 'dateFile',
  'console': {
    layout: {
      type: 'pattern',
      pattern: '%[[%p][%z][%x{stack}][%d][%c]%] %m',
      tokens: {
        stack: function (logEvent) {
          if (logEvent.data.length === 1) return '-'
          const d = logEvent.data[0]
          logEvent.data.splice(0, 1)
          return d
        }
      }
    }

  },
  'file': {
    'filename': path.join(think.ROOT_PATH, 'runtime', 'logs', 'file.log'),
    'maxLogSize': 51200,
    'backups': 10
  },
  'dateFile': {
    'level': 'INFO',
    'filename': path.join(think.ROOT_PATH, 'runtime', 'logs', 'file.log'),
    'pattern': '-yyyy-MM-dd',
    'alwaysIncludePattern': false,
    layout: {
      type: 'pattern',
      pattern: '%[[%p][%z][%x{stack}][%d{yyyy-MM-dd hh:mm:ss.SSS}][%c]%] %m',
      tokens: {
        stack: function (logEvent) {
          if (logEvent.data.length === 1) return '-'
          const d = logEvent.data[0]
          logEvent.data.splice(0, 1)
          return d
        }
      }
    }
  }
}
